package com.easy.controller;


import com.easy.bean.ManagerBean;
import com.easy.bean.UserBean;
import com.easy.service.LoginService;
import com.easy.service.userLoginService;
import com.easy.util.CaptchaUtil;
import com.easy.util.Result;
import com.easy.util.SYS;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/userlogin")
@CrossOrigin
public class userLoginController {
    @Autowired
    userLoginService userloginService;

    @GetMapping("/captcha")
    public Result captcha() {
        return Result.success(CaptchaUtil.getCaptcha());
    }
    @PostMapping("/dologin")
    public Result login(UserBean user, @RequestParam("captcha")String captcha, @RequestParam("key") String key){
        //验证码验证
        if(!CaptchaUtil.checkCaptcha(key,captcha)){
            return Result.fail(500,"验证码错误");
        }
        //登录逻辑
        try{
            String token=userloginService.login(user);
            //删除验证码
            return Result.success("登陆成功",token);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail(500,"登陆异常");
        }

    }
    //获取登录信息
    @GetMapping("/logininfo")
    public Result logininfo(HttpServletRequest  request){
        String token=request.getHeader(SYS.TOKEN_AUTHORIATION);
        UserBean userBean=userloginService.logininfo(token);
        return Result.success(userBean);
    }
}




























